package com.lehecai.ucenter.action.server;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.lehecai.core.service.memcached.MemcachedService;
import com.lehecai.core.type.ucenter.EducationDegreeType;
import com.lehecai.core.type.ucenter.MaritalStatus;
import com.lehecai.core.type.ucenter.SexType;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreHttpUtils;
import com.lehecai.ucenter.action.BaseAction;
import com.lehecai.ucenter.entity.permission.User;
import com.lehecai.ucenter.service.permission.UserService;
import com.lehecai.ucenter.utils.AuthorizeSignUtils;
import com.lehecai.ucenter.utils.MemcachedTicket;

public class ChangeInfoSubmitAction extends BaseAction {
	
	private static final long serialVersionUID = -8634484692576627642L;
	
	private MemcachedService memcachedService;
	private UserService userService;
	
	private User user;
	
	private String ticket;
	private Integer maritalStatusValue;
	private Integer sexTypeValue;
	private Integer educationDegreeTypeValue;
	private String callback;
	
	public String handle() {
		if (StringUtils.isEmpty(ticket)) {
			logger.error("ticket为空");
			super.setErrorMessage("ticket不能为空");
			return "failure";
		}
		if (StringUtils.isEmpty(user.getName())) {
			logger.error("姓名为空");
			super.setErrorMessage("姓名不能为空");
			return "changePwd";
		}
		MemcachedTicket memcachedTicket = null;
		try {
			memcachedTicket = (MemcachedTicket) memcachedService.get(ticket);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		if (memcachedTicket == null) {
			logger.error("修改密码未找到对应ticket信息，ticket可能已过期！");
			super.setErrorMessage("修改密码未找到对应ticket信息，ticket可能已过期，请重试！");
			return "failure";
		}
		User sUser = userService.get(Long.valueOf(memcachedTicket.getUid()));
		sUser.setAge(user.getAge());
		sUser.setBirthday(user.getBirthday());
		sUser.setEducationDegree(educationDegreeTypeValue == null ? null : EducationDegreeType.getItem(educationDegreeTypeValue));
		sUser.setEmail(user.getEmail());
		sUser.setMaritalStatus(maritalStatusValue == null ? null : MaritalStatus.getItem(maritalStatusValue));
		sUser.setMobile(user.getMobile());
		sUser.setName(user.getName());
		sUser.setNation(user.getNation());
		sUser.setQq(user.getQq());
		sUser.setSex(sexTypeValue == null ? null : SexType.getItem(sexTypeValue));
		sUser.setTel(user.getTel());
		sUser.setUniversity(user.getUniversity());
		sUser.setUpdatedTime(new Date());
		sUser.setWeibo(user.getWeibo());
		sUser = userService.update(sUser);
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("uid", sUser.getId() + "");
		params.put("ticket", ticket);
		params.put("random", AuthorizeSignUtils.generateRandomParam());
		
		super.setForwardUrl(CoreHttpUtils.getQueryUrl(callback, params, CharsetConstant.CHARSET_UTF8));
		return "forward";
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	public MemcachedService getMemcachedService() {
		return memcachedService;
	}

	public void setMemcachedService(MemcachedService memcachedService) {
		this.memcachedService = memcachedService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Integer getMaritalStatusValue() {
		return maritalStatusValue;
	}

	public void setMaritalStatusValue(Integer maritalStatusValue) {
		this.maritalStatusValue = maritalStatusValue;
	}

	public Integer getSexTypeValue() {
		return sexTypeValue;
	}

	public void setSexTypeValue(Integer sexTypeValue) {
		this.sexTypeValue = sexTypeValue;
	}

	public Integer getEducationDegreeTypeValue() {
		return educationDegreeTypeValue;
	}

	public void setEducationDegreeTypeValue(Integer educationDegreeTypeValue) {
		this.educationDegreeTypeValue = educationDegreeTypeValue;
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

}
